package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.UserDetailMapper;
import com.hqyj.dao.UserInfoMapper;
import com.hqyj.dao.UserRoleRelationMapper;
import com.hqyj.pojo.UserDetail;
import com.hqyj.pojo.UserInfo;
import com.hqyj.pojo.UserInfoParams;
import com.hqyj.pojo.UserRoleRelation;
import com.hqyj.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    //注入dao层
    @Autowired(required = false)
    UserInfoMapper userInfoMapper;

    @Autowired(required = false)
    UserDetailMapper userDetailMapper;

    @Autowired(required = false)
    UserRoleRelationMapper userRoleRelationMapper;
    @Override
    public HashMap<String, Object> select(UserInfo userInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页的参数
        PageHelper.startPage(userInfo.getPage(),userInfo.getRow());
        //2 查询自定义分页的sql
        List<UserInfo> userInfoList = userInfoMapper.select();
        //3 把集合转换成分页对象
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList);
        //当前页的集合
        map.put("userList",pageInfo.getList());
        //数据总条数
        map.put("total",pageInfo.getTotal());
        //页数
        map.put("totalPage",pageInfo.getPages());
        //上一页
        map.put("prePage",pageInfo.getPrePage());
        //下一页
        if((pageInfo.getPrePage()+2)>=pageInfo.getPages()){
            map.put("nextPage",pageInfo.getPages());
        }else{
            map.put("nextPage",pageInfo.getPrePage()+2);
        }

        //当前页
        map.put("curPage",userInfo.getPage());
        return map;
    }

    @Override
    public HashMap<String, Object> add(UserInfoParams userInfoParams) {

        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userInfoParams.getUserName());
        userInfo.setUserPwd(userInfoParams.getUserPwd());
        int num =userInfoMapper.insertSelective(userInfo);
        if(num>0){
            //新增userdetail
            UserDetail userDetail = new UserDetail();
            userDetail.setEmail(userInfoParams.getEmail());
            userDetail.setPhone(userInfoParams.getPhone());
            userDetail.setUrl(userInfoParams.getUrl());
            UserInfo uu = userInfoMapper.selectByName(userInfoParams.getUserName());
            userDetail.setState(0);
            userDetail.setuId(uu.getuId());
           int nn= userDetailMapper.insertSelective(userDetail);
           if (nn>0){
                //新增用户和角色中间表
               //1,2,4
               if(userInfoParams.getRoleId().length()>1){
                  String arr[]= userInfoParams.getRoleId().split(",");
                  List<UserRoleRelation> list = new ArrayList<>();
                  for(int i=0;i<arr.length;i++){
                      UserRoleRelation urr = new UserRoleRelation();
                      urr.setuId(uu.getuId());
                      urr.setrId(Integer.parseInt(arr[i]));
                      list.add(urr);
                  }
                  int dd= userRoleRelationMapper.insertBitch(list);
                  if(dd>0){
                      map.put("info","新增成功");
                      return map;
                  }
               }

           }

        }
        //错误提示
        map.put("info","新增失败");

        return map;
    }

    @Override
    public UserInfo selectByUId(Integer uId) {
        return userInfoMapper.selectByUId(uId);
    }

    @Override
    public HashMap<String, Object> update(UserInfoParams userInfoParams) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //修改userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setuId(userInfoParams.getUId());
        userInfo.setUserName(userInfoParams.getUserName());
        userInfo.setUserPwd(userInfoParams.getUserPwd());
        int num =userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if(num>0) {
            //新增userdetail
            UserDetail userDetail = new UserDetail();
            //userDetail要修改的主键id
            userDetail.setUdId(userInfoParams.getUdId());

            userDetail.setEmail(userInfoParams.getEmail());
            userDetail.setPhone(userInfoParams.getPhone());
            userDetail.setUrl(userInfoParams.getUrl());
            userDetail.setState(0);
            //userDetail的外键id
            userDetail.setuId(userInfoParams.getUId());

            int nn = userDetailMapper.updateByPrimaryKeySelective(userDetail);
            if (nn>0){

                //先删除中间表的要修改的角色信息
                int ddd= userRoleRelationMapper.del(userInfoParams.getUId());
                if(ddd>0){
                    //新增用户和角色中间表
                    //1,2,4
                    if(userInfoParams.getRoleId().length()>1){
                        String arr[]= userInfoParams.getRoleId().split(",");
                        List<UserRoleRelation> list = new ArrayList<>();
                        for(int i=0;i<arr.length;i++){
                            UserRoleRelation urr = new UserRoleRelation();
                            urr.setuId(userInfoParams.getUId());
                            urr.setrId(Integer.parseInt(arr[i]));
                            list.add(urr);
                        }
                        int dd= userRoleRelationMapper.insertBitch(list);
                        if(dd>0){
                            map.put("info","修改成功");
                            return map;
                        }
                    }
                }


            }
        }
        map.put("info","修改失败");
        return map;
    }

}
